<template>
	<view class="tab-content">
		<view class=" uni-common-mt">
			<uni-segmented-control :current="current" :values="tabBars" style-type="button"
				active-color="#18bc9c" @clickItem="onClickItem" />
		</view>
		
		<!-- 滑块视图 -->
		<view v-show="current === 0">
			<scanInvShelves :getkcFlag="true" :inventory="inventory" :order_type="order_type" :shelves="shelves" ref="scanIS" :allotFlag="true" :allot="allot"></scanInvShelves>
			<view class="swiper-button">
				<!-- <button @click="toOk()">完成</button> -->
				<button @click="toDetails()" class="ok_btn">查看明细</button>
				<button @click="$refs.scanIS.toAdd()">添加</button>
			</view>
		</view>
			
		<view v-show="current === 1">
			<scroll-view scroll-y="true">
				<view class="form_list">
					<showMaterialList :dataArr="porderItem" :orderType="order_type" flag='allot' ref="showMaterialList" :showShelves="false"></showMaterialList>
				</view>
			</scroll-view>
		</view>	
		
	</view>
</template>

<script>
	import scanInvShelves from '@/component/scan-inv-shelves.vue'
	import showMaterialList from "@/component/show-material-list.vue"
	import {
		uniSegmentedControl
	} from '@dcloudio/uni-ui'
	export default {
		components: {
			uniSegmentedControl,scanInvShelves,showMaterialList
		},
		data() {
			return {
				current: 0, /* 选中标签栏的序列,默认显示第一个 */
				tabBars:['扫描','调拨明细'],
				
				porderItem:[],//调拨明细
				
				allot:{},//调拨信息
				inventory:{},//物料明细
				shelves:{},
				
				fqty:'',//库存
				real_quantity:0,//实际库存
				user:uni.getStorageSync('user'),
				order_type:16 //调拨
			}
		},
		computed: {},
		watch:{},
		onLoad(options) {
			this.$Func.setBarTitle('添加调拨明细')
			/* 监听传过来的调拨信息 */
			uni.$once("getAllot", (value) => {
				this.allot = value;
				//this.porderItem = []//初始化
			})
			/* 监听传过来的调拨明细 */
			uni.$on("allotItems", (arrs,value) => {
				this.porderItem = arrs;
				this.toOk(value)//点添加按钮，就自动加入数据库
			})
		},
		onShow() {},
		destroyed() {
			uni.$off("allotItems")
		},
		methods: {
			//切换标签内容
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			//添加物料
			toOk(pitem) {
				if(!this.porderItem.length){
					this.$Api.msg('明细不能为空','error');
					return ;
				}
				if(JSON.stringify(this.allot) == '{}'){
					this.$Api.msg('请选择调拨记录','error');
					return ;
				}
				
				this.$Db.dealAllotItem(this.allot.FBillNo,pitem)//this.porderItem
				.then(res=>{
					this.$Api.msg("添加成功",'success');
					//this.porderItem = []
				})
				.catch(err=>{
					this.$Api.msg('添加失败','error');
					this.$Db.addlog('新增调拨明细',err)
				})
			},
			toDetails(){
				let _this = this
				uni.navigateTo({
					url:'/pages/allot/listitem',
						delta:1,
						success() {
							uni.hideLoading()
							uni.$emit("getAllot", _this.allot)
						}
				})
			}
		}
	}
</script>
<style lang="scss">
</style>